package cn.nfwx.tv.widget;

import android.arch.lifecycle.Lifecycle;
import android.arch.lifecycle.LifecycleObserver;
import android.arch.lifecycle.OnLifecycleEvent;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import cn.nfwx.tv.R;
import cn.nfwx.tv.controller.player.LivePlayerController;
import cn.nfwx.tv.helper.NetworkHelper;
import cn.nfwx.tv.helper.ThreadHelper;
import cn.nfwx.tv.util.LogUtil;
import cn.nfwx.tv.util.SpannableStringUtils;


/**
 * 请求状态view
 * Created by chan on 2017/5/21 0021.
 */
public class LoadView extends RelativeLayout implements View.OnClickListener, LifecycleObserver {
    public static final int DEFAULT = 0;
    public static final int NONET = 1;
    public static final int NODATAS = 2;
    public static final int LOADING = 3;
    public static final int NET4G = 4;
    public static final int LOADERROR = 5;
    public static final int PLAYCOMPLETE = 6;

    public void setOnLoadClickLisener(OnLoadClickListener onLoadClickLisener) {
        this.onLoadClickLisener = onLoadClickLisener;
    }

    public void setTextColor(int color) {
        load_text.setTextColor(color);
    }


    public interface OnLoadClickListener {
        void onreload();

        void onnext();
    }

    private OnLoadClickListener onLoadClickLisener;

    int stats;

    LinearLayout ll_network;
    ImageView progress;
    TextView load_text;
    TextView load_text2;
    TextView load_refresh;
    TextView load_next;
    View v;


    public int getStats() {
        return stats;
    }

    /**
     * 是否加载中
     *
     * @return
     */
    public boolean isLoading() {
        return stats == LOADING;
    }

    public LoadView(Context context) {
        super(context);
        init(context);
    }

    public LoadView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public LoadView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_load, this);
        progress = findView(R.id.progress_icon, view);
        progress.setVisibility(INVISIBLE);
        load_text = findView(R.id.load_text, view);
        load_text.setVisibility(INVISIBLE);
        load_text2 = findView(R.id.load_text2, view);
        load_text2.setVisibility(INVISIBLE);
        load_refresh = findView(R.id.load_refresh, view);
        load_refresh.setOnClickListener(this);
        load_next = findView(R.id.load_next, view);
        load_next.setOnClickListener(this);
        v = findView(R.id.v, view);
        //ll_network = findView(R.id.ll_network, view);
        //ll_network.setVisibility(GONE);
    }

    public void startLoading() {
        stats = LOADING;
        progress.setVisibility(VISIBLE);
        //progress.setImageResource(R.drawable.anim_loading_view);
        progress.setImageResource(R.mipmap.mine_round);
        progress.clearAnimation();
        Animation operatingAnim = AnimationUtils.loadAnimation(getContext(), R.anim.rotate_anim);
        LinearInterpolator lin = new LinearInterpolator();
        operatingAnim.setInterpolator(lin);
        progress.startAnimation(operatingAnim);
        load_text.setVisibility(View.INVISIBLE);
        load_text2.setVisibility(View.INVISIBLE);
        hiddenLoadRefresh();
    }

    public void startLoading(ViewGroup viewGroup) {
        if (getParent() == null) {
            viewGroup.addView(this, viewGroup.getChildCount());
        }
        startLoading();
    }

    public void stopLoading() {
        stats = DEFAULT;
        progress.setVisibility(INVISIBLE);
        progress.clearAnimation();
        load_text.setVisibility(View.INVISIBLE);
        load_text2.setVisibility(View.INVISIBLE);
        hiddenLoadRefresh();
    }

    public void stopLoading(ViewGroup viewGroup) {
        if (getParent() == null) {
            viewGroup.removeView(this);
        }
        stopLoading();
    }

    /**
     * 加载失败
     */
    public void loadError() {
        if (!NetworkHelper.getInstance().hasConnection(load_text.getContext())) {
            netError();
            return;
        }
        stats = LOADERROR;
        load_text.setVisibility(View.VISIBLE);
        load_text.setText(R.string.fail);
        progress.clearAnimation();
        progress.setImageResource(R.mipmap.player_fail);
        progress.setVisibility(VISIBLE);
        load_text2.setVisibility(View.INVISIBLE);
        showLoadRefresh();
    }

    public void loadError(ViewGroup viewGroup) {
        if (getParent() == null) {
            viewGroup.addView(this, viewGroup.getChildCount());
        }
        stopLoading();
        loadError();
    }

    /**
     * 查找界面的部分显示错误
     */
    public void loadErrorSearch() {
        load_text.setVisibility(View.VISIBLE);
        load_text.setText(R.string.fail_search);
        progress.clearAnimation();
        progress.setImageResource(R.mipmap.player_fail);
        progress.setVisibility(GONE);
        load_text2.setVisibility(View.GONE);
        showLoadRefresh();
    }

    public void loadNodata() {
        stats = NODATAS;
        load_text.setVisibility(View.VISIBLE);
        load_text.setText(load_text.getContext().getString(R.string.nodata));
        progress.clearAnimation();
        progress.setImageResource(R.mipmap.player_noresult);
        progress.setVisibility(VISIBLE);
        load_text2.setVisibility(View.INVISIBLE);
        hiddenLoadRefresh();
    }

    public void loadNodata(String text1, String text2) {
        stats = NODATAS;
        load_text.setVisibility(View.VISIBLE);
        load_text.setText(text1);
        load_text.setTextColor(Color.parseColor("#666666"));
        progress.clearAnimation();
        progress.setImageResource(R.mipmap.player_noresult);
        hiddenLoadRefresh();
        progress.setVisibility(VISIBLE);
        load_text2.setVisibility(View.VISIBLE);
        load_text2.setText(text2);
    }

    private void hiddenLoadRefresh() {
        load_refresh.setVisibility(INVISIBLE);
        load_refresh.setClickable(false);
    }

    private void showLoadRefresh() {
        load_refresh.setVisibility(VISIBLE);
        load_refresh.setClickable(true);
        load_refresh.setText(R.string.refresh);
        load_refresh.setBackgroundResource(R.drawable.bg_r100_0c);
        load_refresh.setTextColor(Color.parseColor("#FFFFFF"));
        load_next.setVisibility(View.GONE);
        v.setVisibility(View.GONE);
//        Drawable nav_up = getResources().getDrawable(R.mipmap.button_renovate);
//        nav_up.setBounds(load_refresh.getWidth() / 5, 0, nav_up.getMinimumWidth() + load_refresh.getWidth() / 5, nav_up.getMinimumHeight());
//        load_refresh.setCompoundDrawables(nav_up, null, null, null);
    }

    private void showLoadRefresh2() {
        load_refresh.setVisibility(VISIBLE);
        load_refresh.setClickable(true);
        load_refresh.setText(R.string.network_4g_sure);
        load_refresh.setBackgroundResource(R.drawable.shape_white_100_bg);
        load_refresh.setTextColor(Color.parseColor("#ec1a42"));
        load_refresh.setCompoundDrawables(null, null, null, null);
        load_next.setVisibility(View.GONE);
        v.setVisibility(View.GONE);
    }

    private void showLoadRefresh3(boolean b, int fromState) {
        if (fromState == LivePlayerController.FROM_REVIEW) {
            load_refresh.setVisibility(VISIBLE);
            load_refresh.setClickable(true);
            load_refresh.setText("播放列表");
            load_refresh.setBackgroundResource(R.drawable.shape_white_100_bg);
            load_refresh.setTextColor(Color.parseColor("#ec1a42"));
            load_refresh.setCompoundDrawables(null, null, null, null);
            if (b) {
                load_next.setVisibility(View.VISIBLE);
                load_next.setClickable(true);
                load_next.setText("播放下一个");
                load_next.setBackgroundResource(R.drawable.shape_white_100_bg);
                load_next.setTextColor(Color.parseColor("#ec1a42"));
                load_next.setCompoundDrawables(null, null, null, null);
                v.setVisibility(View.VISIBLE);
            } else {
                load_next.setVisibility(View.GONE);
                load_next.setClickable(true);
                load_next.setText("播放下一个");
                load_next.setBackgroundResource(R.drawable.shape_white_100_bg);
                load_next.setTextColor(Color.parseColor("#ec1a42"));
                load_next.setCompoundDrawables(null, null, null, null);
                v.setVisibility(View.GONE);
            }
        } else {
            load_refresh.setVisibility(View.GONE);
            load_next.setVisibility(View.GONE);
            v.setVisibility(View.GONE);
        }
    }

    /**
     * 没有网络
     */
    public void netError() {
        stats = NONET;
        progress.setVisibility(VISIBLE);
        load_text.setVisibility(View.VISIBLE);
        load_text.setText(R.string.nointernet);
        load_text2.setVisibility(View.INVISIBLE);
        showLoadRefresh();
        progress.clearAnimation();
        progress.setImageResource(R.mipmap.player_nointernet);
    }

    /**
     * 没有网络
     *
     * @param viewGroup
     */
    public void netError(ViewGroup viewGroup) {
        if (getParent() == null) {
            viewGroup.addView(this, viewGroup.getChildCount());
        }
        netError();
    }

    /**
     * 去掉
     *
     * @param viewGroup
     */
    public void removeParentView(ViewGroup viewGroup) {
        if (getParent() != null) {
            viewGroup.removeView(this);
        }
    }

    /**
     * 没有网络
     */
    public void net4gError() {
        stats = NET4G;
        progress.setVisibility(VISIBLE);
        load_text.setVisibility(View.VISIBLE);
        load_text.setText(R.string.network_4g);
        load_text2.setVisibility(View.INVISIBLE);
        progress.clearAnimation();
        progress.setImageResource(R.mipmap.player_nointernet);
        showLoadRefresh2();
    }

    public void net4gError(ViewGroup viewGroup) {
        if (getParent() == null) {
            viewGroup.addView(this, viewGroup.getChildCount());
        }
        net4gError();
    }

    /**
     * 播放结束
     *
     * @param b
     */
    public void playComplete(boolean b, int fromState) {
        stats = PLAYCOMPLETE;
        progress.setVisibility(VISIBLE);
        load_text.setVisibility(View.VISIBLE);
        load_text.setText("播放结束");
        load_text2.setVisibility(View.INVISIBLE);
        progress.setVisibility(View.GONE);
        showLoadRefresh3(b, fromState);
    }

    public void playComplete(ViewGroup viewGroup, boolean b, int fromState) {
        if (getParent() == null) {
            viewGroup.addView(this, viewGroup.getChildCount());
        }
        playComplete(b, fromState);
    }

    private <T extends View> T findView(int rid, View view) {
        return (T) view.findViewById(rid);
    }

    @Override
    public void onClick(final View v) {
        switch (v.getId()) {
            case R.id.load_refresh:
                if (onLoadClickLisener != null) {
                    if (stats != PLAYCOMPLETE) {
                        progress.setVisibility(VISIBLE);
                        //progress.setImageResource(R.drawable.anim_loading_view);
                        progress.setImageResource(R.mipmap.mine_round);
                        Animation operatingAnim = AnimationUtils.loadAnimation(getContext(), R.anim.rotate_anim);
                        LinearInterpolator lin = new LinearInterpolator();
                        operatingAnim.setInterpolator(lin);
                        progress.setAnimation(operatingAnim);
                        progress.startAnimation(operatingAnim);
                        load_refresh.setVisibility(View.GONE);
                        ThreadHelper.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                onLoadClickLisener.onreload();
                            }
                        }, 1000);
                    } else {
                        onLoadClickLisener.onreload();
                    }
                }
                break;
            case R.id.load_next:
                if (onLoadClickLisener != null) {
                    onLoadClickLisener.onnext();
                }
                break;
        }

    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    public void release() {
        onLoadClickLisener = null;
    }
}
